package com.freedy.concurrent.Monitors.patterns;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Freedy
 * @date 2021/4/22 13:28
 */
public class MailBox {
    private static final Map<Long, Guarded> boxes = new ConcurrentHashMap<>();

    private static long id = 1;

    //产生唯一id
    private static synchronized long generateId() {
        return id++;
    }

    public static Guarded createGuarded() {
        Guarded guarded = new Guarded(generateId());
        boxes.put(guarded.getId(), guarded);
        return guarded;
    }

    public static Guarded getGuarded(long id){
        return boxes.remove(id);
    }

    public static Set<Long> getIds() {
        return boxes.keySet();
    }
}
